Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add specific type to Mockito argument matcher any() #27

Merged
merged 15 commits into from
Mar 28, 2024

Conversation

zhangfj88
Copy link
Contributor

add specific type to Mockito argument matcher any()

zhangfj88 and others added 7 commits March 18, 2024 17:19
# Conflicts:
#	src/main/java/com/weirddev/testme/intellij/template/context/MockitoMockBuilder.java
add specific type to Mockito argument matcher any()
add specific type to Mockito argument matcher any()
@CLAassistant
Copy link

CLAassistant commented Mar 20, 2024

CLA assistant check
All committers have signed the CLA.

@@ -240,6 +241,10 @@ private String deductMatcherTypeMethod(Param param, Language language) {
if (language != Language.Scala) {
matcherType += "()";
}
// add specific type to any()
if( matcherType.equals("any()")){
matcherType = addSpecificType(param.getType().getName());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • need to use param.getType().getCanonicalName() otherwise it may not compile.
    For instance in updated UT file - \testData\testMeGenerator\mockReturned\test\com\example\services\impl\FooTest.java :
    when(fooFighter.surrender(any(Fear.class), any(Ice.class), ... Fear & Ice types from package com.example.foes - are not imported

  • won't work for array types. now that I think of it... there's an existing issue with array types when default mappings exists in TYPE_TO_ARG_MATCHERS ....

  • avoid mutations - it's a bad practice.

please use following implementation. it should resolve issues mentioned above:

    @NotNull
    private String deductMatcherTypeMethod(Param param, Language language) {
        Type type = param.getType();
        String matcherMethod = resolveMatcherMethod(type);
        if (language == Language.Scala) {
            return matcherMethod;
        }
        else if (!type.isPrimitive() &&  "any".equals(matcherMethod)) {
            return matcherMethod + "("+ type.getCanonicalName()+(type.isArray()? "[]":"") +".class)";
        } else {
            return matcherMethod+"()";
        }
    }

    private static String resolveMatcherMethod(Type type) {
        if (type.isVarargs()) {
            return "anyVararg";
        } else if(!type.isArray() && TYPE_TO_ARG_MATCHERS.containsKey(type.getCanonicalName())){
            return TYPE_TO_ARG_MATCHERS.get(type.getCanonicalName());
        } else {
            return "any";
        }
    }

BTW, better replace existing signature of deductMatcherTypeMethod with resolveMatcherTypeMethod(Type type, Language language) - caller should pass param.getType() instead of param

Thanks for contributing! happy to see this logic improving

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree with the solution you proposed and am currently attempting to make the modifications,Thanks

@yaronyam
Copy link
Member

@zhangfj88 - please let me know how you wish to progress this PR. There's a pending comment. Do you plan to handle it? If you don't have time to do so - let me know, I might handle it later. Thanks

@zhangfj88
Copy link
Contributor Author

I agree with the solution you proposed and am currently attempting to make the modifications,Thanks

zhangfj88 and others added 4 commits March 26, 2024 16:15
add specific type to Mockito argument matcher any(),and  work for array types
add specific type to Mockito argument matcher any(),and  work for arr…
}


String addSpecificType(String typeName) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

eventually left unused, method can be removed, along with the import statement introducing dependency on apache commons

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks,I will handle it.

}

@SuppressWarnings("unused")
@Deprecated
public boolean shouldStub(Method testMethod, List<Field> testedClassFields) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please keep this method for now. Although its not used. some users might still use it in their custom templates. so it's only marked as @deprecated for now

zhangfj88 and others added 4 commits March 28, 2024 15:04
# Conflicts:
#	testData/testMeGenerator/mockReturned/test/com/example/services/impl/FooTest.java
#	testData/testMeGenerator/mockReturned/testJunit5/com/example/services/impl/FooTest.java
#	testData/testMeGenerator/mockReturned/testTestNg/com/example/services/impl/FooTest.java
add specific type to Mockito argument matcher any-- remove unused method
@yaronyam yaronyam merged commit c33c108 into wrdv:master Mar 28, 2024
5 checks passed
@yaronyam
Copy link
Member

Released in TestMe plugin version 6.4

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants